﻿namespace ShikijuHome.Models
{
    using System;
    using System.Linq;
    using System.Web.Security;

    public class AdminRoleProvider : RoleProvider
    {
        public override string[] GetRolesForUser(string username)
        {
            var usertable = new Users();
            var user = usertable.All("WHERE username = @0", args: username).FirstOrDefault();
            var roletable = new Roles();
            object roleid = user.RoleId; // Very weird, u can put in "2" but a int variable will give runtimes: /
            var roles = roletable.All("WHERE id = @0", args: roleid).FirstOrDefault();
            if (roles == null)
                return null;
            string[] rolesArray = new string[1];
            rolesArray[0] = roles.Name;
            return rolesArray;
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var usertable = new Users();
            var user = usertable.All("WHERE username = @0", args: username).FirstOrDefault();
            var roletable = new Roles();
            var role = roletable.All("WHERE Name = @0", args: roleName).FirstOrDefault();

            if (user.RoleId == role.id)
                return true;
            return false;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}